from unorderList import Node

class OrderedList:
    '''
    有序列表, 使用链表实现
    '''
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def size(self):
        current = self.head
        count = 0
        while current!=None:
            count += 1
            current = current.next
        return count

    def add(self, item):
        temp = Node(item)
        previous = None
        current = self.head 
        completed = False
        while current != None and not completed:
            # 查找插入位置
            if current.data > item:
                completed = True
            else:
                previous=current
                current=current.next
        if previous==None:
            self.head = temp
        else:
            temp.next = current
            previous.next = temp

    def search(self, item):
        found = False
        current = self.head
        while current!=None and not found:
            if current.data < item:
                current = current.next
            elif current.data == item:
                found = True
            else:
                break 
        return found 

# ol = OrderedList()
# # print(ol.isEmpty())

# ol.add(11)
# # print(ol.isEmpty())
# ol.add(19)
# ol.add(17)

# cur=ol.head
# while cur!=None:
#     print(cur.data)
#     cur = cur.next

# # print(ol.search(17))
